package com.chuanyang.driverh.base;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.widget.SystemBarTintManager;


/**基础Activity 所有的Activity都继承此类*/
public abstract class BaseTitleActivity extends BaseActivity {

	protected SystemBarTintManager tintManager;
	private LinearLayout llContent;
	public TextView mBaseTitle, mBaseEnsure, mBaseBack;
	public LinearLayout mRightLayout;
	private LinearLayout mActivityLoading;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setStatusBarColor();//沉浸式导航栏
			setContentView(R.layout.base_titlebar_activity);//设置基本布局
			setContentActivity();//设置activity布局
			setStatusBarSpace();//设置沉浸式导航栏间距。
			initView(savedInstanceState);
			initData();
			initListener();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//获取填充布局id
	protected abstract int getContentResId() ;
	protected abstract void initView(Bundle savedInstanceState);
	protected abstract void initData();
	protected abstract void initListener();

	/**
	 * title返回图片按钮，此处开发者可自定义设置图片资源
	 */
	protected void setBackImg() {
		mBaseBack.setVisibility(View.VISIBLE);
//		mBaseBack.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_back),null,null,null);
		// 初始化返回按钮图片大小
//		UiHelpers.setTextViewIcon(this, mBaseBack, R.drawable.ic_back,
//				R.dimen.common_titlebar_icon_width,
//				R.dimen.common_titlebar_icon_height, UiHelpers.DRAWABLE_LEFT);
	}

	@TargetApi(Build.VERSION_CODES.KITKAT)
	protected void setStatusBarColor() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			tintManager = new SystemBarTintManager(this);
			tintManager.setStatusBarTintEnabled(true);
//			tintManager.setStatusBarTintColor(getResources().getColor(R.color.gray_cc));
			tintManager.setStatusBarTintColor(getResources().getColor(R.color.title_bar));
		}
	}

	private void setStatusBarSpace() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			View statusBar = findViewById(R.id.base_titlebar_status_padding);
			LogUtils.i("tint" + tintManager.getConfig().getStatusBarHeight());
			statusBar.setPadding(0, tintManager.getConfig()
					.getStatusBarHeight(), 0, 0);
		}
	}

	protected void setContentActivity() {
		llContent = (LinearLayout) findViewById(R.id.base_titlebar_content);
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(getContentResId(), null);


		llContent.addView(v, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));
		baseInitView();
	}
	private void baseInitView() {
		mBaseBack = (TextView) findViewById(R.id.base_titlebar_back);
		mBaseTitle = (TextView) findViewById(R.id.base_titlebar_text);
		mBaseEnsure = (TextView) findViewById(R.id.base_titlebar_ensure);

		mBaseEnsure.setOnClickListener(this);
		mBaseBack.setOnClickListener(this);

		mRightLayout = (LinearLayout) findViewById(R.id.base_titlebar_rightlayout);
		mActivityLoading = (LinearLayout) findViewById(R.id.ll_base_activityloading);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.base_titlebar_back:
				mBaseEnsure.requestFocus();
				onBackPressed();
				break;
			case R.id.base_titlebar_ensure:
				baseGoEnsure();
				break;
			default:
				break;
		}
		super.onClick(v);
	}

	/**
	 * 右侧的按钮事件
	 */
	protected void baseGoEnsure() {

	}

	protected void hideTitleBar(){
		findViewById(R.id.base_titlebar_layout).setVisibility(View.GONE);
	}
	protected void showTitleBar(){
		findViewById(R.id.base_titlebar_layout).setVisibility(View.VISIBLE);
	}

	/**
	 * 为了避免页面闪烁可以先显示一个固定布局，加载成功后隐藏掉
	 *
	 * @return
	 */
	protected void showActivityLoading() {
		mActivityLoading = (LinearLayout)findViewById(R.id.ll_base_activityloading);
		mActivityLoading.setVisibility(View.VISIBLE);
		llContent.setVisibility(View.GONE);
	}
	protected  void hideActivityLoading()
	{
		llContent.setVisibility(View.VISIBLE);
		mActivityLoading.setVisibility(View.GONE);
	}

	/**
	 * 获取标题控件
	 *
	 * @return
	 */
	public TextView getTitleTextView() {
		return mBaseTitle;
	}

	/**
	 * 获取右上角空间
	 *
	 * @return
	 */
	public TextView getRightTextView() {
		return mBaseEnsure;
	}
}
